#!/bin/sh
# see also http://www.enricozini.org/2008/tips/lightweight-autologin/

if [ ! -x /usr/bin/X ]; then
	echo "SKIP autologin: X not installed" >&2
	exit 0
fi

USER=altlinux
if ! id "$USER" >&/dev/null; then
     echo "SKIP autologin: $USER does not exist"
     exit 0
fi

add_nopasswdlogin_group()
{
	[ -f "/etc/pam.d/$1" ] &&
	grep -E -qs '^auth.+user[[:blank:]]+ingroup[[:blank:]]+nopasswdlogin' "/etc/pam.d/$1" &&
	groupadd -r nopasswdlogin &&
	gpasswd -a "$USER" nopasswdlogin
}

BAK=".save"
backup() {
	[ -f "$1" -a ! -f "$1$BAK" ] || return 1
	cp -av "$1" "$1$BAK"
}

## this is only relevant for installable livecd
### FIXME: this shouldn't proceed in other cases!
restore_later() {
	[ -n "$1" ] || return 0

	local prefix="/usr/lib/alterator/hooks/livecd-preinstall.d"
	[ -d "$prefix" ] || mkdir -p "$prefix"

	local hook="$prefix/51-disable-autologin-again.sh"
	[ -s "$hook" ] || cat >> "$hook" <<- EOF
	#!/bin/sh
	. livecd-functions
	EOF

	echo mv -v "\$destdir$1$BAK" "\$destdir$1" >> "$hook"
	chmod +x "$hook"
}

## autologin^2
AUTOLOGIN_CFG=/etc/sysconfig/autologin
if [ -x /usr/sbin/autologin ]; then
	backup "$AUTOLOGIN_CFG"
	cat >> "$AUTOLOGIN_CFG" <<- EOF
	USER=$USER
	AUTOLOGIN=yes
	EOF
	restore_later "$AUTOLOGIN_CFG"
fi

## lightdm autologin
LIGHTDM_CONF=/etc/lightdm/lightdm.conf
if [ -f "$LIGHTDM_CONF" ]; then
	mkdir -p /etc/lightdm/lightdm.conf.d
	cat > /etc/lightdm/lightdm.conf.d/autologin.conf <<AUTOLOGIN_LIGHTDM
[Seat:*]
autologin-user=altlinux
autologin-user-timeout=0
AUTOLOGIN_LIGHTDM
	default_session="$(ls -1 /usr/share/xsessions/*.desktop | sed -n 's/^.*\/\([^/]\+\)\.desktop$/\1/;1p')"
	if [ -n "$default_session" ]; then
		echo "autologin-session=$default_session" >> /etc/lightdm/lightdm.conf.d/autologin.conf
	else
		echo "** WARNING: no sessions found in /usr/share/xsessions/" >&2
	fi
	backup "/etc/group"
	add_nopasswdlogin_group lightdm ||:

	restore_later "/etc/group"
fi

## gdm autologin
for GDM_CONF in /etc/gdm/custom.conf /etc/X11/gdm/custom.conf; do
	[ -f "$GDM_CONF" ] || continue
	backup "$GDM_CONF"
	sed -i -e '/\[daemon\]/aAutomaticLoginEnable=true\nAutomaticLogin='$USER \
		"$GDM_CONF"
	backup "/etc/group"
	add_nopasswdlogin_group gdm ||:

	restore_later "$GDM_CONF"
	restore_later "/etc/group"
done

## lxdm autologin
LXDM_CONF=/etc/lxdm/lxdm.conf
if [ -f "$LXDM_CONF" ]; then
	backup "$LXDM_CONF"
	sed -i -e "s/\# autologin=dgod/autologin=$USER/" \
		"$LXDM_CONF"
	backup "/etc/group"
	add_nopasswdlogin_group lxdm ||:

	restore_later "$LXDM_CONF"
	restore_later "/etc/group"
fi

autologin_kdm() {
	backup "$1"
	sed -i \
		-e '/AutoLoginEnable/ s,^.*$,AutoLoginEnable=true,' \
		-e '/AutoLoginUser/ s,^.*$,AutoLoginUser='$USER',' \
		"$1"
	restore_later "$1"
}

## kdm3 autologin
# FIXME: tde packages have kdmrc in /usr via an absolute symlink :-(
KDM_ETC=/etc/X11/kdm
KDM_USR=/usr/share/kde/config/kdm
if [ "$(readlink "$KDM_ETC")" = "$KDM_USR" ]; then
	rm -f "$KDM_ETC"
	ln -s ../.."$KDM_USR" "$KDM_ETC"
fi

KDMRC=/etc/X11/kdm/kdmrc
if [ -f "$KDMRC" ]; then autologin_kdm "$KDMRC"; fi

## kdm4 autologin
KDMRC=/etc/X11/kdm4/kdmrc
if [ -f "$KDMRC" ]; then autologin_kdm "$KDMRC"; fi

## sddm autologin
SDDM_RC=/etc/X11/sddm/sddm.conf
SDDM_SESSION=
if [ -f "$SDDM_RC" ]; then
	backup "$SDDM_RC"
	backup "/etc/group"
	add_nopasswdlogin_group sddm ||:
	sed -i "s,^User=$,&$USER," "$SDDM_RC"
	if [ -f /usr/share/xsessions/lxqt.desktop ]; then
		SDDM_SESSION=lxqt
	elif [ -f /usr/share/xsessions/plasma.desktop ]; then
		SDDM_SESSION=plasma
	fi
	if [ -n "$SDDM_SESSION" ]; then
		sed -i "s,^Session=$,&$SDDM_SESSION," "$SDDM_RC"
	fi
	restore_later "$SDDM_RC"
	restore_later "/etc/group"
fi

## nodm autologin (the last since the most intrusive)
# FIXME nodm and xinitrc should be modified (also in p6)
# WARNING: check user PATH if things go weird!
NODM_RC=/etc/sysconfig/nodm
NODM_EXE=/usr/sbin/nodm
if [ -x "$NODM_EXE" ]; then
	backup "$NODM_RC"
	cat >> "$NODM_RC" <<- EOF
	export NODM_USER=$USER
	EOF
	restore_later "$NODM_RC"
else
	exit 0
fi

# FIXME: drop since #27449 is fixed
PREFDM=/etc/X11/prefdm
if [ -f "$PREFDM" ] && ! grep -q nodm "$PREFDM"; then
	backup "$PREFDM"
	sed -i \
		-e '/AUTOLOGIN_EXE=/ s,^.*$,AUTOLOGIN_EXE='$NODM_EXE',' \
		-e '/AUTOLOGIN_CFG=/ s,^.*$,AUTOLOGIN_CFG='$NODM_RC',' \
		"$PREFDM"
	restore_later "$PREFDM"
fi

# FIXME: drop when #27451 is fixed
NODM_PAM=/etc/pam.d/nodm
for module in pam_console.so pam_ck_connector.so; do
	if [ -f /lib*/security/$module ]; then
		str="-session	optional	$module"
		grep -q "^$str$" "$NODM_PAM" || echo -e "$str" >> "$NODM_PAM"
	fi
done

NODM_POLKIT_CONFDIR=/etc/polkit-1/localauthority/50-local.d
NODM_POLKIT_CONF=org.freedesktop.NetworkManager.pkla
if [ -d "$NODM_POLKIT_CONFDIR" ]; then
	cat > "$NODM_POLKIT_CONFDIR/$NODM_POLKIT_CONF" << _EOF_
[nm-applet]
Identity=unix-group:users
Action=org.freedesktop.NetworkManager.*
ResultAny=yes
ResultInactive=no
ResultActive=yes
_EOF_
fi

NODM_DBUS_NM_CONF=/etc/dbus-1/system.d/nm-applet.conf
if [ -f "$NODM_DBUS_NM_CONF" ]; then
	sed -i "s/at_console=\"true\"/user=\"$USER\"/" "$NODM_DBUS_NM_CONF"
fi

NODM_G2KR_DIR=/etc/skel/.gnome2/keyrings
mkdir -p "$NODM_G2KR_DIR"
echo -n login > "$NODM_G2KR_DIR"/default
cat > "$NODM_G2KR_DIR"/login.keyring << _EOF_
[keyring]
display-name=login
ctime=0
mtime=0
lock-on-idle=false
lock-after=false
_EOF_

:
